#include "StateManager.h"
#include "AbstractState.h"

//--------------------------------------------------------------------------------
StateManager::StateManager():
        QObject(),
        _currentState(NULL)
{
    _states = new QList<AbstractState *>();

}
//--------------------------------------------------------------------------------
StateManager::~StateManager()
{
    foreach(AbstractState* state, *_states)
    {
        delete state;
    }
    delete _states;
}
//--------------------------------------------------------------------------------
void StateManager::setActiveState(AbstractState* state)
{

}
//--------------------------------------------------------------------------------
void StateManager::stateActivated()
{
    qDebug("StateManager before cast");

    AbstractState* state = dynamic_cast<AbstractState *> (sender());

    qDebug("StateManager after cast");

    if (state == _currentState)
        return;

    qDebug("1");

    if (_currentState != NULL)
        _currentState->deactivate();

    qDebug("2");

    if (state)
    {
        qDebug("3.1");
        qDebug("Current state is: " + state->getName().toLatin1());
        _currentState = state;
    }
    else
    {
        qDebug("Wrong sender()!");
    }
}
//--------------------------------------------------------------------------------
void StateManager::addNewState(AbstractState* state)
{
    qDebug(state->getName().toLatin1() + " new state added");
    _states->append(state);
    emit newStateAdded(state);
    connect(state, SIGNAL(activated()), this, SLOT(stateActivated()));
}
//--------------------------------------------------------------------------------
void StateManager::mapClicked(float x, float y)
{
    if (_currentState)
        _currentState->mapClicked(x, y);
}
//--------------------------------------------------------------------------------
